<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['produce:jtPlanMaintain:excel']"
        >
          导出
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      :height="tableH"
      v-loading="loading"
      :data="moeDetList"
      :cell-style="iCellStyle"
      show-summary
      :summary-method="getSummaries"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column
        label="#"
        width="40"
        align="center"
        type="index"
        fixed="left"
      />
      <el-table-column
        label="产品客户编码"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itm_custwldm"
        fixed="left"
      />
      <el-table-column
        label="产品物料代码"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="som_wldm"
      />
      <el-table-column
        label="机台"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_jtbh"
      />
      <el-table-column
        label="组合ID"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="gll_glid"
      />
      <el-table-column
        label="生产物料代码"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_wldm"
      />
      <el-table-column
        label="生产物料品名规格"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itm_pmgg"
      />
      <el-table-column
        label="计划数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_scsl"
        fixed="right"
      />
      <el-table-column
        label="良品数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_lpsl"
        fixed="right"
      />
      <el-table-column
        label="未完成数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_wwcs"
        fixed="right"
      />
      <el-table-column
        label="入库数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_rksl"
        fixed="right"
      />
      <el-table-column
        label="调整数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_tzsl"
        fixed="right"
      />
      <el-table-column
        label="调机不良"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_tjbl"
      />
      <el-table-column
        label="工程不良"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_wxbl"
      />
      <el-table-column
        label="生产不良"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_zcbl"
      />
      <el-table-column
        label="不良总数"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_blsl"
      />
      <el-table-column
        label="生产不良率"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_blv"
      />
      <el-table-column
        label="序号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_scxh"
      />
      <el-table-column
        label="销售单号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_sodh"
      />
      <el-table-column
        label="组别"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_bbdm"
      />
      <el-table-column
        label="制造单号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_zzdh"
      >
        <template #default="scope">
          <PopProductBom :cldZzdh="scope.row.moe_zzdh" />
        </template>
      </el-table-column>
      <el-table-column
        label="排程日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="moe_scrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.moe_scrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="完工日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="moe_wgrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.moe_wgrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="交货日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="som_jhrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.som_jhrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="生产备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_desc"
      />
      <el-table-column
        label="净重(KG)"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itm_jz"
      />
      <el-table-column
        label="欠料重量(KG)"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_qlzl"
      />
      <el-table-column
        label="标准产能"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_cnsl"
      />
      <el-table-column
        label="剩余工时(H)"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_sygs"
      />
      <el-table-column
        label="申请用时(H)"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_sqys"
      />
      <el-table-column
        label="模具编号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_mjbh"
      />
      <el-table-column
        label="模具名称"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="mjm_mjmc"
      />
      <el-table-column
        label="实际穴数"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_xs"
      />
      <el-table-column
        label="标准穴数"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itd_xs"
      />
      <el-table-column
        label="成型时间(S)"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_cxsj"
      />
      <el-table-column
        label="存放位置"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_jtbh"
      />
      <el-table-column
        label="入库仓库"
        :show-overflow-tooltip="true"
        width="130"
        align="center"
        prop="moe_ckdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.moe_ckdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="入库物料"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_rkwl"
      />
      <el-table-column
        label="入库品名规格"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moe_rkpmgg"
      />
      <el-table-column
        label="实际上线时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="moe_sxrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.moe_sxrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="实际下线时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="moe_xxrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.moe_xxrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="预计上线时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="moe_kssj"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.moe_kssj, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="预计下线时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="moe_jssj"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.moe_jssj, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="moe_jlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.moe_jlrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="排程日期" prop="dateRange">
            <el-date-picker
              clearable
              size="small"
              v-model="dateRange"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item label="建立日期" prop="dateRange">
            <el-date-picker
              clearable
              size="small"
              v-model="dateRange"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"></el-date-picker>
          </el-form-item>
        </el-col> -->

        <el-col :span="12">
          <el-form-item label="制造单号" prop="moeZzdh">
            <el-input
              v-model="queryParams.moeZzdh"
              placeholder="请输入制造单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售单号" prop="moeSodh">
            <el-input
              v-model="queryParams.moeSodh"
              placeholder="请输入销售单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台编号" prop="moeJtbh">
            <el-input
              v-model="queryParams.moeJtbh"
              placeholder="机台编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具编号" prop="moeMjbh">
            <el-input
              v-model="queryParams.moeMjbh"
              placeholder="请输入模具编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="itmPmgg" label-width="95px">
            <span slot="label">
              <el-tooltip
                content="综合查询包含物料代码，品名规格，客户编码查询"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              综合查询
            </span>
            <el-input
              v-model="queryParams.itmPmgg"
              clearable
              size="small"
              type="textarea"
              style="width: 450px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <Checkbox v-model="queryParams.params.flag1">未达成</Checkbox>
          <Checkbox v-model="queryParams.params.flag2">生产中</Checkbox>
          <Checkbox v-model="queryParams.params.flag3">未结案</Checkbox>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listPlanReport, exportMoeDet } from "@/api/produce/moeDet";
import { getAll } from "@/api/repertory/Storage";
import PopProductBom from "../../planData/jtPlanMaintain/PopProductBom.vue";
export default {
  name: "PlanMaintainReport",
  components: { PopProductBom },
  data() {
    return {
      /** 颜色事件 */
      iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
        //过账
        if (row.som_ztbz == "4") {
          return "color:#bb0331";
        } else if (row.som_ztbz == "3") {
          return "color:#FF44AA";
        } else if (row.moe_ztbz == "1") {
          return "color:#09aa46";
        }
      },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      tableH: document.body.clientHeight - 230,
      // 非单个禁用
      single: true,
      // 非双个禁用
      doubles: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 工单表格数据
      moeDetList: [],
      // 日期范围
      dateRange: [
        this.$moment().startOf("month").format(),
        this.$moment().add(1, "year").endOf("month").format(),
      ],
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        moeZzdh: null,
        moeSodh: null,
        moeJtbh: null,
        moeMjbh: null,
        itmPmgg: null,
        params: {
          flag1: false,
          flag2: false,
          flag3: false,
        },
      },
      //所有仓库
      allStorageOptions: [],
    };
  },
  created() {
    getAll().then((response) => {
      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    this.getList();
  },
  methods: {
    /** 查询工单列表 */
    getList() {
      this.loading = true;
      if (this.dateRange !== null) {
        this.queryParams.params["beginDate"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endDate"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listPlanReport(this.queryParams).then((response) => {
        this.moeDetList = [];
        this.moeDetList = this.moeDetList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listPlanReport(this.queryParams).then((response) => {
          this.moeDetList = this.moeDetList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.moeDetList = [];
      this.getList();
      this.openSearch = false;
    },
    handleSearch() {
      this.title = "查询工单";
      this.openSearch = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有工单数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportMoeDet(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计:";
          return;
        }
        if (
          ![
            "moe_scsl",
            "moe_lpsl",
            "moe_wwcs",
            "moe_rksl",
            "moe_tzsl",
          ].includes(column.property) ||
          !column.property
        ) {
          sums[index] = " ";
          return;
        }

        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return parseFloat((prev + curr).toFixed(2));
            } else {
              return prev;
            }
          }, 0);
        } else {
          sums[index] = "N/A";
        }
      });

      return sums;
    },
  },
};
</script>

<style lang="scss" scoped></style>
